/**
 * Desc:抓取股票数据入口
 * Author：Kevin
 * CreatedOn: 2021/4/21
 * Version: 1.0
 */

const moment = require('moment');

const {
    MarketService,
    StockCoreService,
    StockHistoryService
} = require('./eastmoney');


function schedule(task) {
    setTimeout(task, 1000 * Math.random());
}

var ps = {
    pn: 1,
    pz: 20,
    fs: 'm:0 t:6,m:0 t:80,m:1 t:2,m:1 t:23' // 沪深A数据
};

var stocks = [];

var cb = (svc, res) => {

    res.objects && res.objects.forEach(stock => {
        // 设置日期字段
        stock.date = moment(Date.now()).format('YYYY-MM-DD');
        // 按下面条件排除一些数据
        stock.isn = stock.price == '-' || stock.price < 1 || stock.roe == '-';
        if (!stock.isn) stocks.push(stock);
    });

    // 判断是否需要加载下一页数据
    if (res.data && (res.data.total / ps.pz > ps.pn)) {
        ps.pn++;
        console.info(`[info] request next page ...`);
        return schedule(() => svc.start(ps, cb));
    }

    stocks.forEach(stock => {
        // 抓取股票全量数据
        schedule(() => {
            // 抓取历史数据
            console.info(`[info] request history data for ${stock.name}(${stock.code})...`);
            new StockHistoryService(stock.market, stock.code, './data/eastmoney/stock').start({ 'klt': '101' }, (s, r) => {
                r.objects.forEach(o => {
                    o.market = stock.market;
                    o.code = stock.code;
                });
                schedule(() => {
                    // 抓取核心财务数据
                    console.info(`[info] request core finance data for ${stock.name}(${stock.code})...`);
                    new StockCoreService(stock.market, stock.code, './data/eastmoney/core').start({}, (s, r) => {
                        r.objects.forEach(o => {
                            // 按下面条件排除一些数据
                            o.isn = o.mv == '-' || o.total == '-' || o.sales == '-' || o.roe == '-';
                            if (o.isn) return;
                            o.market = stock.market;
                            o.code = stock.code;
                            o.name = stock.name;
                        });
                    });
                });
            });
        });
    });
}

console.info(`[task] request task start ...`);
new MarketService('./data/eastmoney/market').start(ps, cb);